package com.qkl.xiatang.app.dto.feed;

import com.alibaba.fastjson.JSON;
import com.qkl.xiatang.app.entity.feed.FeedAllocConfig;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.appenum.FeedAllocSerialEnum;
import com.qkl.xiatang.app.utils.appenum.FeedAllocStatusEnum;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 添加配料
 */
@Data
public class FeedAllocAddDTO extends FeedAllocAddBaseDTO {

    private List<FeedAddDTO> night;

    private List<FeedAddDTO> morning;

    private List<FeedAddDTO> noon;

    @Override
    public List<String> getFeedIds(){
        List<String> feedIdList = new ArrayList<>();
        this.getFeedIds(feedIdList,night);
        this.getFeedIds(feedIdList,morning);
        this.getFeedIds(feedIdList,noon);
        feedIdList = AppUtils.stringsDistinct(feedIdList);
        return feedIdList;
    }

    private void getFeedIds(List<String> feedIdList,List<FeedAddDTO>  feedAddDTOList){
        if(ObjectUtils.isNotEmpty(feedAddDTOList)){
            for(FeedAddDTO dto : feedAddDTOList){
                feedIdList.add(dto.getFeedId());
            }
        }
    }


    @Override
    public boolean checkParams(){
        if(ObjectUtils.isEmpty(night) || ObjectUtils.isEmpty(morning) || ObjectUtils.isEmpty(noon)){
            return false;
        }
        return checkParams2();
    }

    public List<FeedAllocDetailDTO> toDetailList(String userId,String userName ,String pondId ,String companyId,Date allocDate,List<FeedAllocConfig>  configList,int checkSeparTime,List<FeedAllocQtyDTO> feedAllocQtyList){
        List<FeedAllocDetailDTO> detailDTOS = new ArrayList<>();
        for(FeedAllocConfig config : configList) {
            Date nextAllocDate = AppUtils.addDays(allocDate,1);
            FeedAllocDetailDTO detailDTO = new FeedAllocDetailDTO();
            String detailId = AppUtils.genIdStr(detailDTO);
            detailDTO.setId(detailId);
            detailDTO.setAllocUserId(userId);
            detailDTO.setAllocUserName(userName);
            detailDTO.setPondId(pondId);
            detailDTO.setCompanyId(companyId);
            detailDTO.setName(config.getName());
            detailDTO.setSerialNo(config.getSerialNo());
            detailDTO.setCheckSeparTime(checkSeparTime);
            detailDTO.setCurrAllocDate(allocDate);
            detailDTO.setStatus(FeedAllocStatusEnum.NOT.getStatus());
            detailDTO.setMemo( StringUtils.trimToEmpty(this.getMemo()));
            detailDTO.setTemplateId(config.getTemplateId());
            if(FeedAllocSerialEnum.isNight(config.getSerialNo())){
                detailDTO.setAllocDate(allocDate);
                detailDTO.setStartTime( config.toStartTime(allocDate));
                detailDTO.setEndTime( config.toEndTime(allocDate));
            }else {
                detailDTO.setAllocDate(nextAllocDate);
                detailDTO.setStartTime(config.toStartTime(nextAllocDate));
                detailDTO.setEndTime(config.toEndTime(nextAllocDate));
            }

            if(FeedAllocSerialEnum.isNight(config.getSerialNo())){
                detailDTO.setFeedId(feedSort(night));
                //detailDTO.setQty(night.get(0).getQty());
                addQtyList(detailDTO,night,feedAllocQtyList);
            }else if(FeedAllocSerialEnum.isMorning(config.getSerialNo())){
                detailDTO.setFeedId(feedSort(morning));
                //detailDTO.setQty(morning.get(0).getQty());
                addQtyList(detailDTO,morning,feedAllocQtyList);
            }else if(FeedAllocSerialEnum.isNoon(config.getSerialNo())){
                detailDTO.setFeedId(feedSort(noon));
                //detailDTO.setQty(noon.get(0).getQty());
                addQtyList(detailDTO,noon,feedAllocQtyList);
            }
            detailDTOS.add(detailDTO);
        }

        return detailDTOS;
    }

    private static void addQtyList(FeedAllocDetailDTO detailDTO,List<FeedAddDTO> feedAddList,List<FeedAllocQtyDTO> feedAllocQtyList){
        if(ObjectUtils.isEmpty(feedAddList)){
            detailDTO.setQty(0.0);
            detailDTO.setStatus(FeedAllocStatusEnum.UN_NEED.getStatus());
            return;
        }
        Double qty = 0.0;
        String companyId = detailDTO.getCompanyId();
        String pondId = detailDTO.getPondId();
        Integer serialNo = detailDTO.getSerialNo();
        Date allocDate = detailDTO.getAllocDate();
        for(FeedAddDTO feedAddDTO : feedAddList){
            FeedAllocQtyDTO qtyDTO = new FeedAllocQtyDTO();
            qtyDTO.setId(AppUtils.genIdStr(qtyDTO));
            qtyDTO.setQty( (null == feedAddDTO.getQty() ? 0:feedAddDTO.getQty() ));
            qtyDTO.setFeedId(feedAddDTO.getFeedId());
            qtyDTO.setAllocDate(allocDate);
            qtyDTO.setPondId(pondId);
            qtyDTO.setCompanyId(companyId);
            qtyDTO.setSerialNo(serialNo);

            feedAllocQtyList.add(qtyDTO);
            qty = AppUtils.addAmt(qty,feedAddDTO.getQty());
        }
        detailDTO.setQty(qty);
        if(qty == 0.0){
            detailDTO.setStatus(FeedAllocStatusEnum.UN_NEED.getStatus());
        }
        if(feedAddList.size() > 1 ){
            detailDTO.setFeedNameLog("混合");
        }
    }

    private static String feedSort(List<FeedAddDTO> feedAddDTOList){
        List<String> feedIdList = new ArrayList<>();
        for (FeedAddDTO addDTO : feedAddDTOList){
            feedIdList.add(addDTO.getFeedId());
        }
        return feedIdSort(feedIdList);
    }

    private static String feedIdSort(List<String> feedIdList){
        Collections.sort(feedIdList);
        return String.join(",", feedIdList);
    }

    public static void main(String[] args) {
        String feedId = "1732696797141184514";
        FeedAllocAddDTO dto = new FeedAllocAddDTO();
        dto.setTime("2023-12-11");
        dto.setGroupIdList(new ArrayList<>());

        System.out.println(JSON.toJSONString(dto));

    }

}
